{% extends 'index.html' %}
{% block page-content %}
<div id="page-wrapper">
    <div class="row">
         <div class="col-lg-12">
              <h1 class="page-header"><i class="fa  fa-print  "></i> SQL执行记录</h1>
         </div>
                <!-- /.col-lg-12 -->
    </div>
	<div class="row">
                <div class="col-lg-12">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                           	<i class="fa   fa-gear"></i> 操作日志
                        </div>
                        <div class="panel-body">

				                            
				           <ul class="nav nav-tabs">
				              <li class="active"><a href="#profile" data-toggle="tab"><i class="fa  fa-github-alt"></i> SQL执行记录</a>
				              </li>			              
				           </ul>
				
				                            
				           <div class="tab-content">
				              <div class="tab-pane fade in active" id="profile">
				                   <div class="row">
				                   		<div class="col-lg-12">
				                   			<br>
				                            <table width="100%" class="table table-striped table-bordered table-hover" id="sqlogsTableList">
				                                <thead>
				                                    <tr>
				                                    	<th class="text-center">#</th>
				                                    	<th class="text-center">环境</th>
				                                    	<th class="text-center">数据库</th>
				                                    	<th class="text-center">SQL内容</th>
				                                    	<th class="text-center">执行用户</th>
				                                    	<th class="text-center">执行结果</th>
														<th class="text-center">执行时间</th>
														<th class="text-center">失败原因</th>
				                                        {% if perms.OpsManage.can_delete_sql_execute_histroy %}
				                                        	<th class="text-center">操作</th>
				                                        {% endif %}														
				                                    </tr>
				                                </thead>
				                                <tbody>
				                                	{% for ds in sqlLogsList %}
				                                		<tr class="odd gradeX" id="sqlLogs-{{ds.id}}">
				                                			<td class="text-center">{{ds.id}} </td>
				                                			
				                                			<td class="text-center">
                                								{% if ds.exe_db.db_env == 'test' %}
		                                							<span class="label label-success">测试</span>
		                                						{% else %}	                     				
		                                							<span class="label label-info">生产</span>
		                                						{% endif %}				                                			
				                                			</td>
				                                			<td class="text-center">
																{{ds.exe_db.db_name}} - ({{ds.exe_db.db_host}})
				                                			</td>
															<td class="text-center">
																<a href="javascript:;" target="_blank" class="tooltip-test" data-toggle="tooltip" title="{{ds.exe_sql}}">{{ds.exe_sql|slice:":40" }}...</a>
															</td>				                                			
				                                			<td class="text-center">{{ds.exe_user}} </td>
				                                			<td class="text-center">
                                								{% if ds.exec_status == 1 %}
		                                							<span class="label label-success">成功</span>
		                                						{% else %}	                     				
		                                							<span class="label label-danger">失败</span>
		                                						{% endif %}					                                			
				                                			</td>			                                			
				                                			<td class="text-center">{{ds.create_time|date:"Y/m/d H:i  "}} </td>
				                                			<td class="text-center">
				                                				<a href="javascript:;" target="_blank" class="tooltip-test" data-toggle="tooltip" title="{{ds.exe_result}}">{{ds.exe_result|default:""|slice:":10"}}</a>
				                                				
				                                			</td>
				                                			{% if perms.OpsManage.can_delete_sql_execute_histroy %}
					                                			<td class="text-center">
					                                				<a href="javascript:" onclick="deleteSQLogs(this,{{ds.id}})">
						                                				<button class="btn btn-danger btn-xs">					                                					
																			<i class="glyphicon glyphicon-trash  bigger-110 icon-only"></i>
																		</button>
																	</a>
																</td>
															{% endif %}				                                			
				                                		</tr>
				                                	{% endfor %}
				                                </tbody>
				                            </table>				       
										{% if sqlLogsList %}
											<ul class="pagination">
											        {% if sqlLogsList.has_previous %}
											            <li><a href="/db/sql/logs/{{ sqlLogsList.previous_page_number }}/">&laquo;</a></li>
											        {% endif %}
											        {% for page in sqlLogsList.paginator.page_range %}
											        	{% if sqlLogsList.number == page %}
											        		<li class="disabled"><a href="/db/sql/logs/{{page}}/">{{page}}</a></li>
											        	{% else %}
											        		<li><a href="/db/sql/logs/{{page}}/">{{page}}</a></li>
											        	{% endif %}
											        {% endfor %}
											        {% if sqlLogsList.has_next %}
											            <li><a href="/db/sql/logs/{{ sqlLogsList.next_page_number }}/">&raquo;</a></li>
											        {% endif %}
											</ul>
                     		            {% endif %}		
										</div>
				                   </div>
				              </div>				              
				           </div>


                            <!-- /.row (nested) -->
                        </div>
                        <!-- /.panel-body -->
                    </div>
                    <!-- /.panel -->
                </div>
                <!-- /.col-lg-12 -->
            </div>    
</div>


<script type="text/javascript">


		$(document).ready(function() {
		    $('#sqlogsTableList').DataTable({
		        responsive: true,
		        "order": [[1, 'asc']]
		    });
		});

/* 		function deleteSQLogs(obj,id) {
			$.ajax({
				dataType: "JSON",
				url:'/api/logs/sql/'+ id +'/', //请求地址
				type:"DELETE",  //提交类似
				success:function(response){
					var str= 'sqlLogs-' + id
					document.getElementById(str).innerHTML = ''; 
				}					
			})					
		}	 */
		
		function deleteSQLogs(obj,id){
			var txt=  "是否确认删除？";
			var btnObj = $(obj);
			btnObj.attr('disabled',true);
			var option = {
				title: "删除记录",
				btn: parseInt("0011",2),
				onOk: function(){
					$.ajax({
						  type: 'DELETE',
						  url: '/api/logs/sql/'+id+'/',
					      success:function(response){	
					    	btnObj.removeAttr('disabled');
/* 		                	window.wxc.xcConfirm("删除成功！", window.wxc.xcConfirm.typeEnum.success); */
							var str= 'sqlLogs-' + id
							document.getElementById(str).innerHTML = ''; 			            
					      },
			              error:function(response){
			            	btnObj.removeAttr('disabled');
			            	window.wxc.xcConfirm("删除失败！", window.wxc.xcConfirm.typeEnum.error);	
			              }
						});
				},
				onCancel:function(){	
				},
				onClose:function(){
				}
			}
			window.wxc.xcConfirm(txt, "custom", option);			   
		} 		
  
</script>

{% endblock %}